package com.yserp.project.erp.order.selfpurchrigistration.controller;

import com.yserp.common.utils.poi.ExcelUtil;
import com.yserp.common.utils.text.Convert;
import com.yserp.framework.aspectj.lang.annotation.Log;
import com.yserp.framework.aspectj.lang.enums.BusinessType;
import com.yserp.framework.web.controller.BaseController;
import com.yserp.framework.web.domain.AjaxResult;
import com.yserp.framework.web.page.TableDataInfo;
import com.yserp.project.erp.order.selfpurchrigistration.domain.SelfPurchRigistration;
import com.yserp.project.erp.order.selfpurchrigistration.service.ISelfPurchRigistrationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * 自购物料登记Controller
 * 
 * @author yserp
 * @date 2022-08-09
 */
@Controller
@RequestMapping("/order/selfpurchrigistration")
public class SelfPurchRigistrationController extends BaseController
{
    private String prefix = "erp/order/selfpurchrigistration";

    @Autowired
    private ISelfPurchRigistrationService selfPurchRigistrationService;

    @RequiresPermissions("order:selfpurchrigistration:view")
    @GetMapping()
    public String selfpurchrigistration()
    {
        return prefix + "/selfpurchrigistration";
    }

    /**
     * 查询自购物料登记列表
     */
    @RequiresPermissions("order:selfpurchrigistration:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SelfPurchRigistration selfPurchRigistration)
    {
        startPage();
        List<SelfPurchRigistration> list = selfPurchRigistrationService.selectSelfPurchRigistrationList(selfPurchRigistration);
        return getDataTable(list);
    }

    /**
     * 导出自购物料登记列表
     */
    @RequiresPermissions("order:selfpurchrigistration:export")
    @Log(title = "自购物料登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SelfPurchRigistration selfPurchRigistration)
    {
        List<SelfPurchRigistration> list = selfPurchRigistrationService.selectSelfPurchRigistrationList(selfPurchRigistration);
        ExcelUtil<SelfPurchRigistration> util = new ExcelUtil<SelfPurchRigistration>(SelfPurchRigistration.class);
        return util.exportExcel(list, "自购物料登记数据");
    }

    /**
     * 新增自购物料登记
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存自购物料登记
     */
    @RequiresPermissions("order:selfpurchrigistration:add")
    @Log(title = "自购物料登记", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SelfPurchRigistration selfPurchRigistration)
    {
        return toAjax(selfPurchRigistrationService.save(selfPurchRigistration));
    }

    /**
     * 修改自购物料登记
     */
    @RequiresPermissions("order:selfpurchrigistration:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SelfPurchRigistration selfPurchRigistration = selfPurchRigistrationService.getById(id);
        mmap.put("selfPurchRigistration", selfPurchRigistration);
        return prefix + "/edit";
    }

    /**
     * 修改保存自购物料登记
     */
    @RequiresPermissions("order:selfpurchrigistration:edit")
    @Log(title = "自购物料登记", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SelfPurchRigistration selfPurchRigistration)
    {
        return toAjax(selfPurchRigistrationService.updateById(selfPurchRigistration));
    }

    /**
     * 删除自购物料登记
     */
    @RequiresPermissions("order:selfpurchrigistration:remove")
    @Log(title = "自购物料登记", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(selfPurchRigistrationService.removeByIds(Arrays.asList(Convert.toLongArray(ids))));
    }
}
